#!/bin/sh

####################
###### Post Install Script
####################

##########
##### Modify the apache config file, taking exceptions for itools and os x server users
##########

#####
##### Variables
#####
aVers=`httpd -v | grep -c 'Apache/2.2'`
oVers=`sw_vers | grep -c 10.6`

##### OS X Leopard
if [ ${aVers} = 1 ]; then
	apacheVers="2.2"
	apachePath="/private/etc/apache2"
	apacheConf="${apachePath}/httpd.conf"
	apacheMods="/usr/libexec/apache2"
	lassoFolder="apache2.2"
	connector="Lasso8ConnectorforApache2.2.so"

        ##### 10.6
        if [ ${oVers} = 1 ]; then
                osVer="10.6/"
        else
                osVer="10.5/"
        fi

##### OS X Tiger
else
	apacheVers="1.3"
	apachePath="/private/etc/httpd"
	apacheConf="${apachePath}/httpd.conf"
	apacheMods="/usr/libexec/httpd"
	lassoFolder="apache"
	connector="Lasso8ConnectorforApache.so"
	osVer=""
fi


##### We need to move this file out because it'll screw things up
if [ -e "${apachePath}/users/lasso.conf" ]; then
	mv "${apachePath}/users/lasso.conf" "${apachePath}/lasso.conf.old"
fi

mv -f "/tmp/lassolibs8.5/${lassoFolder}/${osVer}${connector}" "${apacheMods}"


##########
##### If the lasso8.conf file already exists, do nothing.
##########
if [ ! -e "${apachePath}"/users/lasso8.conf ]; then
	mkdir -p "${apachePath}/users/"
	mv "/tmp/lassolibs8.5/${lassoFolder}/lasso8.conf" "${apachePath}"/users/
fi

##### If the file exists, remove additions made by Lasso
if [ -f "${apacheConf}" ]; then

	##### Remove the lasso additions to the httpd.conf file if they are present
	while [ $? = 0 ];
	do
		ed - "${apacheConf}" > /dev/null 2>&1 << 'LASSOREMOVE' > /dev/null 2>&1
/#Begin: Added by Lasso [0-9]/,/#End: Added by Lasso [0-9]/d
w
q
LASSOREMOVE
	done

	
	##### If we don't have an include path, we need to add one
	if ! ( ( cat "${apacheConf}" | grep -q ^Include.*${apachePath}/users/\?$ ) || 
			( cat "${apacheConf}" | grep -q ^Include.*${apachePath}/users/lasso8.conf$ ) || 
			( cat "${apacheConf}" | grep -q ^Include.*${apachePath}/users/\*\.conf$ ) ) then

		##### See if there is a commented out httpd/users include
		if cat "${apacheConf}" | grep -q ^#Include\ ${apachePath}/users
		then
			
			##### Echo commented out
			cp "${apacheConf}" "${apacheConf}.lassobackup"

			##########
			##### APACHE 2.2
			##########
			if [ ${apacheVers} = "2.2" ]; then
				ed - "${apacheConf}" > /dev/null 2>&1 << 'LASSOADD' > /dev/null 2>&1
/^#Include \/private\/etc\/apach2\/users
a
#Begin: Added by Lasso 8 Installer
Include /private/etc/apache2/users/lasso8.conf
#End: Added by Lasso 8 Installer
.
w
q
LASSOADD
				echo $?
				
				
			##########
			##### APACHE 1.3
			##########
			else
				ed - "${apacheConf}" > /dev/null 2>&1 << 'LASSOADD' > /dev/null 2>&1
/^#Include \/private\/etc\/httpd\/users
a
#Begin: Added by Lasso 8 Installer
Include /private/etc/httpd/users/lasso8.conf
#End: Added by Lasso 8 Installer
.
w
q
LASSOADD
				echo $?
			fi

		else
			##### Echo not found
			cp "${apacheConf}" "${apacheConf}.lassobackup"
			
			##########
			##### APACHE 2.2
			##########
			if [ ${apacheVers} = "2.2" ]; then
				ed - "${apacheConf}" > /dev/null 2>&1 << 'LASSOADD' > /dev/null 2>&1
$
a
#Begin: Added by Lasso 8 Installer
Include /private/etc/apache2/users/lasso8.conf
#End: Added by Lasso 8 Installer
.
w
q
LASSOADD
			
			##########
			##### APACHE 1.3
			##########
			else
				ed - "${apacheConf}" > /dev/null 2>&1 << 'LASSOADD' > /dev/null 2>&1
$
a
#Begin: Added by Lasso 8 Installer
Include /private/etc/httpd/users/lasso8.conf
#End: Added by Lasso 8 Installer
.
w
q
LASSOADD
			fi

		fi
	fi
fi

##########
##### Set permissions
##########
THEUID=`ps -acxouid,command | grep Finder | sed -e 's/[ ]*\([^ ]*\).*/\1/g'`

if [ ! -d "${apacheConf}/users" ]; then
	echo "Adding httpd/users folder"
	mkdir -p         "${apachePath}/users"
	chmod 755        "${apachePath}/users"
	chown root:wheel "${apachePath}/users"
fi

/usr/sbin/apachectl restart 2> /dev/null

if [ ! -f /private/tmp/ls8install ]; then
	if [ "$COMMAND_LINE_INSTALL" = 1 ] || [ -f /private/tmp/lp8windowopen ]; then
		echo "Command line install, not opening window."
	else
		for IP in `ifconfig -u -a inet 2> /dev/null | sed -n 's/.*inet \([0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}\)/\1/pg' | awk '{ print $1 }'`; do

			if [[ `curl -I http://$IP/ServerAdmin.LassoApp --silent` = *Server:\ Lasso*8* ]];
			then
				echo "Opening default browser for user #${THEUID}"
				sudo -u "#${THEUID}" open "http://${IP}/ServerAdmin.LassoApp" > /dev/null 2>&1
				touch /private/tmp/lp8windowopen
				break;
			fi

		done

	fi
fi

echo ""
